package com.bee.common.tools.sms;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SmsAsyn {
	private static final Logger log = LoggerFactory.getLogger(SmsAsyn.class);

	private	BlockingQueue<Sms> basket = new ArrayBlockingQueue<Sms>(2000);
	
	@Autowired
	private ALSMSUtils smsUtil;

	public void addSmsAsyn(Sms  sms) {
		basket.offer(sms);
	}
	
	
	@PostConstruct
	public void excuteAsyn(){
		
		Executors.newSingleThreadExecutor().execute(new Runnable() {
			@Override
			public void run() {
				while(true){
					Sms  notify;
					try {
						notify = basket.take();
						smsUtil.sendSms(notify);
					}catch(Exception e){
						log.error("错误信息",e);
					}
				}
			}
		});	
	}
}
